package com.example.choupaiapp.main.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.choupaiapp.main.bean.SendCodeBean;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.fragment.CommonFragment;
import com.example.choupaiapp.common.util.ContextUtil;
import com.example.choupaiapp.common.util.DialogUtil;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.common.view.TabEntity;
import com.example.choupaiapp.common.view.statusbar.StatusView;
import com.example.choupaiapp.main.activity.mine.FollowFasActivity;
import com.example.choupaiapp.main.activity.mine.InviteNewActivity;
import com.example.choupaiapp.main.activity.mine.MineBgActivity;
import com.example.choupaiapp.main.activity.mine.MineCommentActivity;
import com.example.choupaiapp.main.activity.mine.MineEnergyActivity;
import com.example.choupaiapp.main.activity.mine.MineFabulousActivity;
import com.example.choupaiapp.main.activity.mine.MineHeadPhotoActivity;
import com.example.choupaiapp.main.activity.mine.MineWalletActivity;
import com.example.choupaiapp.main.activity.mine.SettingActivity;
import com.example.choupaiapp.main.activity.mine.SignActivity;
import com.example.choupaiapp.main.bean.SignBean;
import com.example.choupaiapp.main.bean.UserInfoBean;
import com.example.choupaiapp.main.constant.Config;
import com.example.choupaiapp.main.constant.Url;
import com.example.choupaiapp.main.fragment.mine.MineAllFragment;
import com.example.choupaiapp.main.fragment.mine.MineImgTextFragment;
import com.example.choupaiapp.main.fragment.mine.MineVideoFragment;
import com.example.choupaiapp.main.test.TestActivity;
import com.example.choupaiapp.main.view.ScaleRelativeLayout;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ASUS on 2019/10/30.
 */

public class MineFragment extends CommonFragment implements OnRefreshLoadMoreListener {

    @BindView(R.id.setting)
    ImageView setting;
    @BindView(R.id.head_photo)
    CircleImageView headPhoto;
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.description)
    TextView description;
    @BindView(R.id.sign_img)
    TextView signImg;
    @BindView(R.id.fabulous_num)
    TextView fabulousNum;
    @BindView(R.id.comment_num)
    TextView commentNum;
    @BindView(R.id.wallet_num)
    TextView walletNum;
    @BindView(R.id.energy_num)
    TextView energyNum;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.linear)
    LinearLayout linear;
    @BindView(R.id.title_ll)
    LinearLayout title_ll;
    @BindView(R.id.username_top)
    TextView username_top;
    @BindView(R.id.vip_img)
    ImageView vip_img;
    @BindView(R.id.follow_num)
    TextView follow_num;
    @BindView(R.id.fans_num)
    TextView fans_num;
    @BindView(R.id.title_rl)
    RelativeLayout title_rl;
    @BindView(R.id.status_view_top)
    StatusView status_view_top;
    @BindView(R.id.mine_bg)
    ScaleRelativeLayout mine_bg;
    @BindView(R.id.invite)
    ImageView invite;
    @BindView(R.id.mine_bg_img)
    ImageView mine_bg_img;
    @BindView(R.id.tablayout)
    CommonTabLayout tabLayout;
    Unbinder unbinder;
    private UserInfoBean mUserInfoBean;
    private String[] mTitles = new String[3];
    private ArrayList<Fragment> mFragments = new ArrayList<>();//存放Fragment
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private MyPagerAdapter myPagerAdapter;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    private int mPosition = 0;
    @BindView(R.id.coordinator)
    CoordinatorLayout coordinator;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @SuppressLint("RestrictedApi")
    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);

        mUserInfoBean = (UserInfoBean) SharedPreferencesHelp.getBean("userInfo");

        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));

        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
                if (Math.abs(i) <= 0) {
                    username_top.setTextColor(getResources().getColor(R.color.transparent));
                    title_rl.setBackgroundColor(getResources().getColor(R.color.transparent));
                    status_view_top.setBackgroundColor(getResources().getColor(R.color.transparent));
                } else if (Math.abs(i) > 0 && Math.abs(i) <= linear.getHeight() / 2) {
                    float f = (float) Math.abs(i) / (float) linear.getHeight();
                    float alpha = (float) 255 * f * 2;
                    username_top.setTextColor(Color.argb((int) alpha, 0, 0, 0));
                    title_rl.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                    status_view_top.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
//                status_view_top.setBackgroundColor(Color.argb((int) alpha, 204, 204, 204));
                } else {
                    username_top.setTextColor(getResources().getColor(R.color.color_333));
                    title_rl.setBackgroundColor(getResources().getColor(R.color.white));
                    status_view_top.setBackgroundColor(getResources().getColor(R.color.white));
//                status_view_top.setBackgroundColor(getResources().getColor(R.color.normal_status));
                }
            }
        });

        mTitles[0] = "全部";
        mTitles[1] = "图片";
        mTitles[2] = "视频";

        tabLayout.setTextSelectColor(getResources().getColor(R.color.color_333));
        tabLayout.setTextUnselectColor(getResources().getColor(R.color.color_8888));
        tabLayout.setIndicatorColor(getResources().getColor(R.color.color_333));
        tabLayout.setIndicatorHeight(2f);
        tabLayout.setIndicatorCornerRadius(1f);
        tabLayout.setIndicatorWidth(40f);
        tabLayout.setTextsize(15);

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        myPagerAdapter = new MyPagerAdapter(getChildFragmentManager());
        viewpager.setAdapter(myPagerAdapter);
        tabLayout.setTabData(mTabEntities);

        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpager.setCurrentItem(position);
                mPosition = position;
                if (position == 0) {
                    MobclickAgent.onEvent(ContextUtil.getContext(), "allID");
                } else if (position == 1) {
                    MobclickAgent.onEvent(ContextUtil.getContext(), "pictureID");
                } else if (position == 1) {
                    MobclickAgent.onEvent(ContextUtil.getContext(), "videoID");
                }
            }

            @Override
            public void onTabReselect(int position) {
            }
        });


        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
                mPosition = position;
                if (position == 0) {
                    MobclickAgent.onEvent(ContextUtil.getContext(), "allID");
                } else if (position == 1) {
                    MobclickAgent.onEvent(ContextUtil.getContext(), "pictureID");
                } else if (position == 1) {
                    MobclickAgent.onEvent(ContextUtil.getContext(), "videoID");
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }


        });

        viewpager.setCurrentItem(0);
        viewpager.setOffscreenPageLimit(3);


        Animation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(1500);
        rotate.setFillAfter(true);
        rotate.setRepeatMode(Animation.RESTART);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.setRepeatCount(Animation.INFINITE);
        invite.startAnimation(rotate);

        return view;
    }


    @Override
    protected void initData() {

//        showDialog(context, null);
        finalOkGo.requestHead(RequestType.GETHEAD, Url.GET_UERINFO, null, Url.GET_UERINFO, new Callback<UserInfoBean>() {
            @Override
            public void onSuccess(UserInfoBean userInfoBean) {
                dismissDialog();
                if (userInfoBean.getCode() == Contacts.SUCCESS) {
                    refreshLayout.finishRefresh();
                    SharedPreferencesHelp.putBean("userInfo", userInfoBean);
                    mUserInfoBean = userInfoBean;
                    username.setText(userInfoBean.getUser().getNickname());
                    if (userInfoBean.getUser().getNickname().length() > 7) {
                        username_top.setText(userInfoBean.getUser().getNickname().substring(0, 7) + "...");
                    } else {
                        username_top.setText(userInfoBean.getUser().getNickname());
                    }
                    description.setText(userInfoBean.getUser().getSignature());
                    follow_num.setText(userInfoBean.getUser().getAttentionSum() + "");
                    fans_num.setText(userInfoBean.getUser().getFansSum() + "");
                    Glide.with(context)
                            .load(userInfoBean.getUser().getAvatar() + Config.IMAGE_LIM)
                            .centerCrop()
                            .error(R.mipmap.icon_no_head)
                            .into(headPhoto);
                    fabulousNum.setText(userInfoBean.getUser().getLikeContentCount() + "");
                    commentNum.setText(userInfoBean.getUser().getCommentCount() + "");
                    walletNum.setText(userInfoBean.getUser().getBalance().stripTrailingZeros().toPlainString() + "");
                    energyNum.setText(userInfoBean.getUser().getEnergy() + "");

                    Glide.with(context)
                            .load(userInfoBean.getUser().getBackgroundImg() + Config.IMAGE_LIM)
                            .centerCrop()
                            .error(R.mipmap.icon_mine_bg)
                            .into(mine_bg_img);

                    if (userInfoBean.getUser().getIsSignIn() == 0) {
                        signImg.setText("签到");
                    } else {
                        signImg.setText("已签到");
                    }

                    EventBus.getDefault().post("refresh_mine_tab_data");

                    if (mFragments.size() < 3) {
                        viewpager.post(new Runnable() {
                            @Override
                            public void run() {

                                MineAllFragment mineAllFragment = MineAllFragment.newInstance(mUserInfoBean, 1 + "", 0);
                                mFragments.add(mineAllFragment);

                                MineImgTextFragment mineImgTextFragment = MineImgTextFragment.newInstance(mUserInfoBean.getUser().getUserId() + "", 1 + "", 0);
                                mFragments.add(mineImgTextFragment);

                                MineVideoFragment mineVideoFragment = MineVideoFragment.newInstance(mUserInfoBean.getUser().getUserId() + "", 1 + "", 0);
                                mFragments.add(mineVideoFragment);

                                myPagerAdapter.notifyDataSetChanged();
                            }
                        });
                    }

                } else {
                    refreshLayout.finishRefresh();
                    if (userInfoBean.getCode() == Contacts.TOKEN_EMPTY) {
                    } else {
                        showImageToas(userInfoBean.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
                refreshLayout.finishRefresh();
            }
        });

    }

    private void requestMineInfo() {
//        showDialog(context, null);
        finalOkGo.requestHead(RequestType.GETHEAD, Url.GET_UERINFO, null, Url.GET_UERINFO, new Callback<UserInfoBean>() {
            @Override
            public void onSuccess(UserInfoBean userInfoBean) {
                dismissDialog();
                if (userInfoBean.getCode() == Contacts.SUCCESS) {
                    refreshLayout.finishRefresh();
                    SharedPreferencesHelp.putBean("userInfo", userInfoBean);
                    mUserInfoBean = userInfoBean;
                    username.setText(userInfoBean.getUser().getNickname());
                    if (userInfoBean.getUser().getNickname().length() > 7) {
                        username_top.setText(userInfoBean.getUser().getNickname().substring(0, 7) + "...");
                    } else {
                        username_top.setText(userInfoBean.getUser().getNickname());
                    }
                    description.setText(userInfoBean.getUser().getSignature());
                    follow_num.setText(userInfoBean.getUser().getAttentionSum() + "");
                    fans_num.setText(userInfoBean.getUser().getFansSum() + "");
                    Glide.with(context)
                            .load(userInfoBean.getUser().getAvatar() + Config.IMAGE_LIM)
                            .centerCrop()
                            .error(R.mipmap.icon_no_head)
                            .into(headPhoto);
                    fabulousNum.setText(userInfoBean.getUser().getLikeContentCount() + "");
                    commentNum.setText(userInfoBean.getUser().getCommentCount() + "");
                    walletNum.setText(userInfoBean.getUser().getBalance().stripTrailingZeros().toPlainString() + "");
                    energyNum.setText(userInfoBean.getUser().getEnergy() + "");

                    Glide.with(context)
                            .load(userInfoBean.getUser().getBackgroundImg() + Config.IMAGE_LIM)
                            .centerCrop()
                            .error(R.mipmap.icon_mine_bg)
                            .into(mine_bg_img);

                    if (userInfoBean.getUser().getIsSignIn() == 0) {
                        signImg.setText("签到");
                    } else {
                        signImg.setText("已签到");
                    }
                } else {
                    if (userInfoBean.getCode() == Contacts.TOKEN_EMPTY) {
                    } else {
                        showImageToas(userInfoBean.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    //    @Override
    @OnClick({R.id.setting, R.id.head_photo, R.id.sign_img, R.id.fabulous, R.id.comment,
            R.id.wallet, R.id.energy, R.id.follow_ll, R.id.fans_ll, R.id.description,
            R.id.mine_bg, R.id.invite})
    public void onViewClicked(View view) {
//        super.onViewClicked(view);
        if (Utils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.setting:
                MobclickAgent.onEvent(ContextUtil.getContext(), "setID");
                startActivity(SettingActivity.class);
                break;
            case R.id.head_photo:
                Bundle bundle = new Bundle();
                bundle.putString("url", mUserInfoBean.getUser().getAvatar());
                bundle.putString("name", username.getText().toString());
                startActivity(MineHeadPhotoActivity.class, bundle);
                MobclickAgent.onEvent(ContextUtil.getContext(), "photoID");
                break;
            case R.id.sign_img:
                if (mUserInfoBean.getUser().getIsSignIn() == 0) {
                    showDialog(context, "签到中...");
                    finalOkGo.requestHead(RequestType.POSTHEAD, Url.SIGN, null, Url.SIGN, new Callback<SignBean>() {
                        @Override
                        public void onSuccess(SignBean signBean) {
                            dismissDialog();
                            if (signBean.getCode() == Contacts.SUCCESS) {
                                MobclickAgent.onEvent(ContextUtil.getContext(), "signID");
                                signImg.setText("已签到");
                                mUserInfoBean.getUser().setIsSignIn(1);
                                Bundle bundle1 = new Bundle();
                                bundle1.putSerializable("signData", signBean);
                                bundle1.putInt("isSign", 2);
                                startActivity(SignActivity.class, bundle1);
                            } else {
                                if (signBean.getCode() == Contacts.TOKEN_EMPTY) {
                                } else {
                                    showImageToas(signBean.getMsg());
                                }
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            dismissDialog();
                        }
                    });
                } else {
                    startActivity(SignActivity.class);
//                    showImageToas("您已经签过到了");
                }
                break;
            case R.id.fabulous:
                startActivity(MineFabulousActivity.class);
                MobclickAgent.onEvent(ContextUtil.getContext(), "myPraiseID");
                break;
            case R.id.comment:
                startActivity(MineCommentActivity.class);
                MobclickAgent.onEvent(ContextUtil.getContext(), "myCommentID");
                break;
            case R.id.wallet:
                startActivity(MineWalletActivity.class);
                MobclickAgent.onEvent(ContextUtil.getContext(), "myWalletID");
                break;
            case R.id.energy:
                startActivity(MineEnergyActivity.class);
                MobclickAgent.onEvent(ContextUtil.getContext(), "myEnergyID");
                break;
            case R.id.follow_ll:
                Bundle bundle3 = new Bundle();
                bundle3.putString("flag", "mine_follow");
                startActivity(FollowFasActivity.class, bundle3);
                MobclickAgent.onEvent(ContextUtil.getContext(), "focusID");
                break;
            case R.id.fans_ll:
                Bundle bundle2 = new Bundle();
                bundle2.putString("flag", "mine_fans");
                startActivity(FollowFasActivity.class, bundle2);
                MobclickAgent.onEvent(ContextUtil.getContext(), "fansID");
                break;
            case R.id.description:
                MobclickAgent.onEvent(ContextUtil.getContext(), "signatureID");
                DialogUtil.getInstance().showTopDialog(context, R.layout.dialog_mine_desc);
                LinearLayout dialog_top_layout = (LinearLayout) DialogUtil.getInstance().dialog_top_layout;
                EditText edit = dialog_top_layout.findViewById(R.id.edit);
                TextView save = dialog_top_layout.findViewById(R.id.save);
                TextView num_count = dialog_top_layout.findViewById(R.id.num_count);

                edit.setText(description.getText().toString());
                if (!TextUtils.isEmpty(description.getText().toString())) {
                    edit.setSelection(description.getText().toString().length());
                }

                edit.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        num_count.setText(editable.toString().length() + "/30");
                    }
                });

                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Utils.isDoubleClick()) {
                            return;
                        }
                        if (!TextUtils.isEmpty(edit.getText().toString().trim())) {
                            showDialog(context, "修改中...");
                            JSONObject jsonObject = new JSONObject();
                            try {
                                jsonObject.put("signature", edit.getText().toString().trim());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.CHANGE_USERINFO, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                                @Override
                                public void onSuccess(SendCodeBean sendCodeBean) {
                                    dismissDialog();
                                    if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                        DialogUtil.getInstance().dialog_top.dismiss();
                                        description.setText(edit.getText().toString().trim());
                                    } else {
                                        if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                        } else {
                                            showImageToas(sendCodeBean.getMsg());
                                        }
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);
                                    dismissDialog();
                                }
                            });
                        } else {
                            showImageToas("请写点什么吧");
                        }
                    }
                });
                break;
            case R.id.mine_bg:
                Bundle bundle1 = new Bundle();
                bundle1.putString("url", mUserInfoBean.getUser().getBackgroundImg());
                bundle1.putString("name", username.getText().toString());
                startActivity(MineBgActivity.class, bundle1);
                MobclickAgent.onEvent(ContextUtil.getContext(), "backgroundID");
                break;
            case R.id.invite:
//                startActivity(TestActivity.class);
                Bundle bundle4 = new Bundle();
                bundle4.putString("inviteCode", mUserInfoBean.getUser().getInviteCode());
                startActivity(InviteNewActivity.class, bundle4);
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void OnMessageEvent(String event) {
        if (event.equals("refresh_mine")) {
            requestMineInfo();
        } else if (event.equals("perfom_invite")) {
            Bundle bundle4 = new Bundle();
            bundle4.putString("inviteCode", mUserInfoBean.getUser().getInviteCode());
            startActivity(InviteNewActivity.class);
        } else if (event.equals("mine_scollTo_top")) {
            scrollToTop();
            EventBus.getDefault().post("scollTo_mine_top:" + mPosition);
        } else if (event.equals("mine_refresh_data")) {
//            scrollToTop();
            refreshLayout.autoRefresh();
            EventBus.getDefault().post("refresh_mine_data:" + mPosition);
        }
    }

    public void scrollToTop() {
        CoordinatorLayout.Behavior behavior =
                ((CoordinatorLayout.LayoutParams) appbar.getLayoutParams()).getBehavior();
        if (behavior instanceof AppBarLayout.Behavior) {
            AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
            int topAndBottomOffset = appBarLayoutBehavior.getTopAndBottomOffset();
            if (topAndBottomOffset != 0) {
                username_top.setTextColor(getResources().getColor(R.color.transparent));
                title_rl.setBackgroundColor(getResources().getColor(R.color.transparent));
                status_view_top.setBackgroundColor(getResources().getColor(R.color.transparent));
                appBarLayoutBehavior.setTopAndBottomOffset(0);
            }
        }
    }
}
